﻿Imports DevExpress.XtraGrid.Views.Grid
Public Class frmCategories
    Dim _mysql As New ClsSQLhelper
    Dim Strsql As String
    Dim Primary_Code As Integer = -1
#Region "Function"
    Sub Load_Data(Optional ByVal Strcriteria As String = "")
        Try
            Strsql = "Select Cate_ID,Cate_Name,Cate_Code from categories"
            If Strcriteria <> "" Then
                Select Case SearchOption.EditValue
                    Case 0
                        Strsql = Strsql & "where  Cate_ID = " & Strcriteria
                    Case 1
                        Strsql = Strsql & "where  Cate_Name Like '" & Strcriteria & "%'"
                End Select

            End If
            Dim DT_Section As New DataTable
            DT_Section = _mysql.GetMYSQLDataTable(Strsql, "categories")
            With GridCategories
                .DataSource = DT_Section
                Me.GridColumn1.FieldName = "Cate_ID"
                Me.GridColumn2.FieldName = "Cate_Name"
                Me.GridColumn3.FieldName = "Cate_Code"
            End With
        Catch ex As Exception

        End Try
        
    End Sub
#End Region
    Private Sub frmCategories_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Load_Data()


    End Sub


    Private Sub MNAddCategories_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MNAddCategories.Click
        If frmNewCategories.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
        Load_Data()

    End Sub

    Private Sub MnDeleteCategories_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnDeleteCategories.Click
        Try
            If Primary_Code = -1 Then Exit Sub
            If (MsgBox("กรุณายืนยันการลบข้อมูล " & Me.TxtSearch.EditValue, MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No) Then Exit Sub
            If (MsgBox("การลบข้อมูลเมื่อยืนยันแล้วจะไม่สามารถนำกลับมาใหม่ได้ คุณยังต้องการยืนยันใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No) Then Exit Sub
            Strsql = "Select count(*) as cc from categories where Cate_ID = '" & Me.TxtSearch.EditValue & "'"
            If (_mysql.MySQLExecuteScalar(Strsql)) = 0 Then
                MsgBox("ไม่พบรหัสที่ต้องการลบ", MsgBoxStyle.Critical, "กรุณาตรวจสอบรายการลบ")
                Exit Sub
            End If


            Strsql = "Delete from categories where  Cate_ID = '" & Me.TxtSearch.EditValue & "'"

            Select Case (_mysql.MySQLExecuteScalar(Strsql))
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "MnDeleteCategories_Click"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                        Exit Sub
                    End With

                Case Else
                    MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการลบข้อมูล")


            End Select

            'Add Log
            Strsql = "Insert into transections_logs (trans_code,trans_datetime,trans_computer,trans_item) values (13,'" & Pn_Framework.MysqlDateTimeFormat(Now, True) & "','" & Pn_Framework.Get_Computername & "','" & Me.TxtSearch.EditValue & "') "
            Select Case (_mysql.MySQLExecute(Strsql))
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "MnDeleteCategories_Click"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()

                    End With
            End Select

            Load_Data()
        Catch ex As Exception
            ' MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub GridView1_CustomRowCellEdit(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEdit
       
    End Sub

    Private Sub GridCategories_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridCategories.Click

    End Sub

    Private Sub BtnSearch_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnSearch.ItemClick
        If Me.TxtSearch.EditValue = "" Then Exit Sub
        Load_Data(Me.TxtSearch.EditValue)
    End Sub

    Private Sub MnEditCategories_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnEditCategories.Click
        Try
            If Primary_Code = -1 Then Exit Sub
            Dim DT_Categories As New DataTable
            DT_Categories = _mysql.GetMYSQLDataTable("select Cate_ID,Cate_name,Cate_Code from categories where cate_id = " & Primary_Code)
            With frmEditCategories
                .TxtCategoriesName.Text = DT_Categories.Rows(0).Item("cate_name").ToString
                .TxtCategoriesCode.Text = DT_Categories.Rows(0).Item("Cate_Code").ToString
                .Primary_Code = Primary_Code
                If .ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub

            End With
            Load_Data()
        Catch ex As Exception

            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "MnEditCategories_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With

        End Try
       
    End Sub

    Private Sub GridView1_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEditForEditing
        Try

            Dim Gv As GridView = sender
            Dim CodeName As String

            'Dim TypeCODE As String = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Item_Type")).ToString()
            If Me.SearchOption.EditValue = 0 Then
                CodeName = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Cate_ID")).ToString()
            Else
                CodeName = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Cate_Name")).ToString()
            End If
            Primary_Code = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Cate_ID")).ToString()

            Me.TxtSearch.EditValue = CodeName
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class